﻿using ETModel;
using System.Collections.Generic;

namespace ETHotfix {
    /// <summary>
    /// 
    /// </summary>
    [ObjectSystem]
    public class MapPlayerAwakeSystem : AwakeSystem<MapPlayer, long> {
        public override void Awake(MapPlayer self, long gateClientActorId) {
            self.Awake(gateClientActorId);
        }
    }

    /// <summary>
    /// 存放于Map服务器的玩家实体,Parent为GateServerSession
    /// </summary>
	public sealed class MapPlayer : Entity {
        /// <summary>
        /// 用户自定义对象
        /// </summary>
        public object UserObject { get; set; }
        /// <summary>
        /// 获取用户自定义对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetUserObject<T>() where T : class { return UserObject as T; }
        /// <summary>
        /// 玩家客户端的actorId(就是GatePlayer的实例ID),(调用MapPlayerLoginSuccess(...)或MapPlayerReconnect(...)初始化的,框架自动赋值)
        /// </summary>
        public long GateClientActorId;
        /// <summary>
        /// 当前玩家是否连线中
        /// </summary>
        public bool IsDisconnect;

        private UnitsComponent PlayerUnitsComponent;
        private UnitsComponent MapUnitsComponent;

        public void Awake(long gateClientActorId) {
            GateClientActorId = gateClientActorId;
            IsDisconnect = false;
            MapUnitsComponent = Game.Scene.GetComponent<UnitsComponent>();
            PlayerUnitsComponent = AddComponent<UnitsComponent>();
        }

        public void AddSelfUnit(Unit unit) {
            MapUnitsComponent.Add(unit);
            PlayerUnitsComponent.Add(unit);
        }
        /// <summary>
        /// 移除并销毁属于自己的单位
        /// </summary>
        /// <param name="unitId"></param>
        public void RemovSelfUnitById(long unitId) {
            MapUnitsComponent.Remove(unitId);
            PlayerUnitsComponent.Remove(unitId);
        }
        public Unit[] GetAllSelfUnits() {
            return PlayerUnitsComponent.GetAll();
        }
        public Unit GetFirstSelfUnit() {
            return PlayerUnitsComponent.GetFirst();
        }

        public override void Dispose() {
            if (this.IsDisposed) {
                return;
            }
            base.Dispose();

            GateClientActorId = 0;
            IsDisconnect = true;

            MapUnitsComponent = null;
            PlayerUnitsComponent = null;
        }
    }
}